home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / asynchat.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  252 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """A class supporting chat-style (command/response) protocols.
  5.  
  6. This class adds support for 'chat' style protocols - where one side
  7. sends a 'command', and the other sends a response (examples would be
  8. the common internet protocols - smtp, nntp, ftp, etc..).
  9.  
  10. The handle_read() method looks at the input stream for the current
  11. 'terminator' (usually '\\r\\n' for single-line responses, '\\r\\n.\\r\\n'
  12. for multi-line output), calling self.found_terminator() on its
  13. receipt.
  14.  
  15. for example:
  16. Say you build an async nntp client using this class.  At the start
  17. of the connection, you'll have self.terminator set to '\\r\\n', in
  18. order to process the single-line greeting.  Just before issuing a
  19. 'LIST' command you'll set it to '\\r\\n.\\r\\n'.  The output of the LIST
  20. command will be accumulated (using your own 'collect_incoming_data'
  21. method) up to the terminator, and then control will be returned to
  22. you - by calling your self.found_terminator() method.
  23. """
  24. import socket
  25. import asyncore
  26. from collections import deque
  27. from sys import py3kwarning
  28. from warnings import filterwarnings, catch_warnings
  29.  
  30. class async_chat(asyncore.dispatcher):
  31.     '''This is an abstract class.  You must derive from this class, and add
  32.     the two methods collect_incoming_data() and found_terminator()'''
  33.     ac_in_buffer_size = 4096
  34.     ac_out_buffer_size = 4096
  35.     
  36.     def __init__(self, sock = None, map = None):
  37.         self.ac_in_buffer = ''
  38.         self.incoming = []
  39.         self.producer_fifo = deque()
  40.         asyncore.dispatcher.__init__(self, sock, map)
  41.  
  42.     
  43.     def collect_incoming_data(self, data):
  44.         raise NotImplementedError('must be implemented in subclass')
  45.  
  46.     
  47.     def _collect_incoming_data(self, data):
  48.         self.incoming.append(data)
  49.  
  50.     
  51.     def _get_data(self):
  52.         d = ''.join(self.incoming)
  53.         del self.incoming[:]
  54.         return d
  55.  
  56.     
  57.     def found_terminator(self):
  58.         raise NotImplementedError('must be implemented in subclass')
  59.  
  60.     
  61.     def set_terminator(self, term):
  62.         '''Set the input delimiter.  Can be a fixed string of any length, an integer, or None'''
  63.         self.terminator = term
  64.  
  65.     
  66.     def get_terminator(self):
  67.         return self.terminator
  68.  
  69.     
  70.     def handle_read(self):
  71.         
  72.         try:
  73.             data = self.recv(self.ac_in_buffer_size)
  74.         except socket.error:
  75.             why = None
  76.             self.handle_error()
  77.             return None
  78.  
  79.         self.ac_in_buffer = self.ac_in_buffer + data
  80.         while self.ac_in_buffer:
  81.             lb = len(self.ac_in_buffer)
  82.             terminator = self.get_terminator()
  83.             None if not terminator else lb < n
  84.             terminator_len = len(terminator)
  85.             index = self.ac_in_buffer.find(terminator)
  86.             if index != -1:
  87.                 if index > 0:
  88.                     self.collect_incoming_data(self.ac_in_buffer[:index])
  89.                 
  90.                 self.ac_in_buffer = self.ac_in_buffer[index + terminator_len:]
  91.                 self.found_terminator()
  92.                 continue
  93.             index = find_prefix_at_end(self.ac_in_buffer, terminator)
  94.             if index:
  95.                 if index != lb:
  96.                     self.collect_incoming_data(self.ac_in_buffer[:-index])
  97.                     self.ac_in_buffer = self.ac_in_buffer[-index:]
  98.                 
  99.                 break
  100.                 continue
  101.             self.collect_incoming_data(self.ac_in_buffer)
  102.             self.ac_in_buffer = ''
  103.  
  104.     
  105.     def handle_write(self):
  106.         self.initiate_send()
  107.  
  108.     
  109.     def handle_close(self):
  110.         self.close()
  111.  
  112.     
  113.     def push(self, data):
  114.         sabs = self.ac_out_buffer_size
  115.         if len(data) > sabs:
  116.             for i in xrange(0, len(data), sabs):
  117.                 self.producer_fifo.append(data[i:i + sabs])
  118.             
  119.         else:
  120.             self.producer_fifo.append(data)
  121.         self.initiate_send()
  122.  
  123.     
  124.     def push_with_producer(self, producer):
  125.         self.producer_fifo.append(producer)
  126.         self.initiate_send()
  127.  
  128.     
  129.     def readable(self):
  130.         '''predicate for inclusion in the readable for select()'''
  131.         return 1
  132.  
  133.     
  134.     def writable(self):
  135.         '''predicate for inclusion in the writable for select()'''
  136.         if not self.producer_fifo:
  137.             pass
  138.         return not (self.connected)
  139.  
  140.     
  141.     def close_when_done(self):
  142.         '''automatically close this channel once the outgoing queue is empty'''
  143.         self.producer_fifo.append(None)
  144.  
  145.     
  146.     def initiate_send(self):
  147.         while self.producer_fifo and self.connected:
  148.             first = self.producer_fifo[0]
  149.             if not first:
  150.                 del self.producer_fifo[0]
  151.                 if first is None:
  152.                     self.handle_close()
  153.                     return None
  154.             
  155.             obs = self.ac_out_buffer_size
  156.             
  157.             try:
  158.                 catch_warnings().__enter__()
  159.                 
  160.                 try:
  161.                     data = buffer(first, 0, obs)
  162.                 finally:
  163.                     pass
  164.  
  165.             except TypeError:
  166.                 data = first.more()
  167.                 if data:
  168.                     self.producer_fifo.appendleft(data)
  169.                     continue
  170.                 del self.producer_fifo[0]
  171.                 continue
  172.  
  173.             
  174.             try:
  175.                 num_sent = self.send(data)
  176.             except socket.error:
  177.                 self.handle_error()
  178.                 return None
  179.  
  180.             if num_sent:
  181.                 if num_sent < len(data) or obs < len(first):
  182.                     self.producer_fifo[0] = first[num_sent:]
  183.                 else:
  184.                     del self.producer_fifo[0]
  185.             
  186.             return None
  187.  
  188.     
  189.     def discard_buffers(self):
  190.         self.ac_in_buffer = ''
  191.         del self.incoming[:]
  192.         self.producer_fifo.clear()
  193.  
  194.  
  195.  
  196. class simple_producer:
  197.     
  198.     def __init__(self, data, buffer_size = 512):
  199.         self.data = data
  200.         self.buffer_size = buffer_size
  201.  
  202.     
  203.     def more(self):
  204.         if len(self.data) > self.buffer_size:
  205.             result = self.data[:self.buffer_size]
  206.             self.data = self.data[self.buffer_size:]
  207.             return result
  208.         result = self.data
  209.         self.data = ''
  210.         return result
  211.  
  212.  
  213.  
  214. class fifo:
  215.     
  216.     def __init__(self, list = None):
  217.         if not list:
  218.             self.list = deque()
  219.         else:
  220.             self.list = deque(list)
  221.  
  222.     
  223.     def __len__(self):
  224.         return len(self.list)
  225.  
  226.     
  227.     def is_empty(self):
  228.         return not (self.list)
  229.  
  230.     
  231.     def first(self):
  232.         return self.list[0]
  233.  
  234.     
  235.     def push(self, data):
  236.         self.list.append(data)
  237.  
  238.     
  239.     def pop(self):
  240.         if self.list:
  241.             return (1, self.list.popleft())
  242.         return (0, None)
  243.  
  244.  
  245.  
  246. def find_prefix_at_end(haystack, needle):
  247.     l = len(needle) - 1
  248.     while l and not haystack.endswith(needle[:l]):
  249.         l -= 1
  250.     return l
  251.  
  252.